import './Login.scss'
import React, { Component } from 'react';
import {user_login} from '../../utils/api'
import { Toast } from 'react-vant';
class login extends Component {
    constructor(props){
        super(props);
        this.state = {
            phone:'',
            pass:'',
        }
    }
    handleChange(e){
        this.setState({
            [e.target.name]:e.target.value
        },()=>{
            console.log(this.state)
        })
    }
    register(){
        // console.log('注册成功')
        if(/^1[3-9]\d{9}$/.test(this.state.phone) == false){
            Toast.fail('请输入合法的手机号');
        }else if( !this.state.pass){
            Toast.fail('请输入密码');
        }else{
            // var newFormData = JSON.parse(JSON.stringify(this.state));
            // delete newFormData.checkpass;
            // console.log(newFormData);
            user_login(this.state).then((res)=>{
                if(res.data.code == 200){
                    Toast.success('登录成功');
                }else{
                    Toast.fail('登录失败');
                }
            })
        }
    }
    render() {
        return (
            <div className='app'>
                <div className='content'>
                <div className='user'>
                    <input type="text" placeholder='请输入用户名' name='phone' value={this.state.phone} onChange={(e)=>{ this.handleChange(e) }}/>
                </div>
                <div className="psw">
                    <input type="password" placeholder='请输入密码' name='pass' value={this.state.pass} onChange={(e)=>{ this.handleChange(e) }}/>
                </div>
                <div className="login">
                <input type="submit" value="登录" onClick={()=>{ this.register() }}/>
                </div>
                <div className='jump'>
                    <p>注册</p>
                </div>
                </div>
            </div>
        );
    }
}

export default login;